package com.cencus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencus.dao.RecordDao;
import com.cencus.dao.RecordTagDao;
import com.cencus.dao.TagDao;
import com.cencus.domain.Record;
import com.cencus.domain.RecordTag;
import com.cencus.domain.Tag;
import com.cencus.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordDao recordDao;

    @Autowired
    private RecordTagDao recordTagDao;

    @Autowired
    private TagDao tagDao;

    @Override
    public Record createRecord(Record record) {
        //System.out.println(record);
        recordDao.insert(record);
        insertRecordTags(record);
        return record;
    }

    @Override
    public Record updateRecord(Record record) {
        recordDao.updateById(record);
        return record;

    }

    @Override
    public boolean deleteRecord(Integer recordId) {
        return recordDao.deleteById(recordId) > 0;
    }

    @Override
    public List<Record> getAllRecord() {
        // 查询所有 record
        List<Record> records = recordDao.selectList(null);

        // 查询每个 record 的关联标签信息
        for (Record record : records) {
            Integer recordId = record.getRecordId();

            // 查询关联的 tag_id
            QueryWrapper<RecordTag> recordTagWrapper = new QueryWrapper<>();
            recordTagWrapper.eq("record_id", recordId);
            List<RecordTag> recordTags = recordTagDao.selectList(recordTagWrapper);

            // 获取关联的标签信息
            List<Integer> tagIds = recordTags.stream().map(RecordTag::getTagId).collect(Collectors.toList());
            if (!tagIds.isEmpty()) {
                QueryWrapper<Tag> tagWrapper = new QueryWrapper<>();
                tagWrapper.in("tag_id", tagIds);
                List<Tag> tags = tagDao.selectList(tagWrapper);
                // 将获取的标签信息设置到 record 的 tags 属性中
                record.setTags(tags);
            }
        }

        return records;
    }

    @Override
    public Record getRecordById(Integer recordId) {
        Record record = recordDao.selectById(recordId);
        QueryWrapper<RecordTag> recordTagWrapper = new QueryWrapper<>();
        recordTagWrapper.eq("record_id", recordId);
        List<RecordTag> recordTags = recordTagDao.selectList(recordTagWrapper);
        List<Integer> collect = recordTags.stream().map(RecordTag::getTagId).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            QueryWrapper<Tag> tagWrapper = new QueryWrapper<>();
            tagWrapper.in("tag_id", collect);
            List<Tag> tags = tagDao.selectList(tagWrapper);
            // 将获取的标签信息设置到 record 的 tags 属性中
            record.setTags(tags);
        }
        return record;
    }

    @Override
    public IPage<Record> selectRecordWithTagsPage(Page<Record> page, QueryWrapper<Record> wrapper) {
        //return recordDao.selectRecordWithTagsPage(page, wrapper);
        return null;
    }

    public boolean associated(@PathVariable Integer recordId, @RequestBody List<Tag> tags){
    //    保证tags全部存在哈
        RecordTag recordTag = new RecordTag();
        recordTag.setRecordId(recordId);
        for (Tag tag : tags) {
            recordTag.setTagId(tag.getTagId());
            recordTagDao.insert(recordTag);
        }
        return true;
    }

    private void insertRecordTags(Record record) {
        List<Tag> tags = record.getTags();
        if (tags != null && !tags.isEmpty()) {
            Integer recordId = record.getRecordId();
            for (Tag tag : tags) {
                // 创建 RecordTag 对象，插入关联表数据
                RecordTag recordTag = new RecordTag();
                recordTag.setRecordId(recordId);
                recordTag.setTagId(tag.getTagId());
                recordTagDao.insert(recordTag);
            }
        }
    }
}
